<?php
/*
 * synthese/load-index.phtml
 */

use RHX\Helper\Number;
?>
<form id="form-synthese" action="#" method="post">
    <input type="hidden" name="periode" value="<?php echo $this->periode; ?>">
    <input type="hidden" name="site" value="<?php echo $this->site; ?>">
    <table class="table table-condensed table-striped list-table">
	<thead>
	    <tr>
		<td colspan="21">
		    <button type="button" class="btn btn-inverse btn-small synthese">Générer le fichier de Synthese</button>
		</td>
	    </tr>
	    <tr>
		<td>Mat.</td>
		<td>Nom</td>
		<td>Prénom</td>
		<td>Site</td>
		<td>Taux</td>
		<td>Heures Contrat</td>
		<!--td>Heures Compl.</td-->
		<td>Heures Compl.</td>
		<td>Heures Sup. 25%</td>
		<td>Heures Abs.</td>
		<td>Heures Abs. CP</td>
		<td>Nb repas</td>
		<td>Prime</td>
		<td>Ind CP & Rég</td>
		<td>Salaire brut</td>
		<td>CP dû N-1</td>
		<td>CP acquis N</td>
		<td>Prov CP</td>
		<td>Salaire Net</td>
		<td>Virt 1</td>
		<td>Virt 2</td>
		<td>Chèque</td>
	    </tr>
	</thead>
	<tbody>
	    <?php foreach ($this->syntheses as $synthese): ?>
                <?php if(!$synthese->id){$synthese->id = -1 ;}; ?>
		<?php
                $idSynth =   $synthese->id ;
		$employe = $synthese->getEmploye();
		$site	 = $employe->getSite();
		$prime	 = $synthese->getPrimeCP();
		?>
    	    <tr data-pk = '<?php echo $idSynth; ?>' class='row-synth row-synth-<?php echo $idSynth ; ?>'>
    		<td>
			<?php echo $employe->getMatricule(true); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][matricule]" value="<?php echo $employe->getMatricule(FALSE); ?>">		    
    		</td>
    		<td>
		    <?php echo $employe->nom1; ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][nom]" value="<?php echo addslashes($employe->nom1); ?>">
		</td>
    		<td>
		    <?php echo $employe->prenom; ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][prenom]" value="<?php echo addslashes($employe->prenom); ?>">
		</td>
    		<td>
		    <?php echo $site->libelle; ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][site]" value="<?php echo addslashes($site->libelle); ?>">
		</td>
    		<td>
		    <?php $taux = $employe->getTaux(); ?>
		    <?php echo Number::frenchFormat($taux); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][taux]" value="<?php echo $taux; ?>">
		</td>
		<!--td>
		    <?php $hebdo = $employe->getHoraireHebdo(); ?>
		    <?php echo Number::frenchFormat($hebdo); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule; ?>][hebdo]" value="<?php echo $hebdo; ?>">
		</td-->
		<td>
		    <?php echo Number::frenchFormat($synthese->mensualite); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule; ?>][mensualite]" value="<?php echo $synthese->mensualite; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->heure_comptabilise); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][heure_comptabilise]" value="<?php echo $synthese->heure_comptabilise; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->heure_comptabilise_heure_supplementaire); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][heure_comptabilise_heure_supplementaire]" value="<?php echo $synthese->heure_comptabilise_heure_supplementaire; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->heure_negative); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][heure_negative]" value="<?php echo $synthese->heure_negative; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->cp_heure); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][cp_heure]" value="<?php echo $synthese->cp_heure; ?>">
		</td>
    		<td>
		    <?php echo (int) $synthese->repas; ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][repas]" value="<?php echo $synthese->repas; ?>">
		</td>
    		<td>
		    <?php echo ($prime) ? Number::frenchFormat($prime->valeur) : ''; ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][valeur]" value="<?php echo ($prime) ? $prime->valeur : '0.0'; ?>">
		</td>
    		<td> 
                    <a class='synth-edit-int' href="#" data-pk='<?php echo $synthese->id ;?>' data-name='heure_module_m1' data-id='<?php echo $employe->matricule;?>' data-group='<?php echo $synthese->group; ?>'>
                       
                        <?php echo Number::frenchFormat($synthese->heure_module_m1); ?>
                    </a>    
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][heure_module_m1]" value="<?php echo $synthese->heure_module_m1; ?>">
		</td>
    		<td>
		    <span class='synth-salaire-text'><?php echo number_format($synthese->salaire_brute, 2, ',', ''); ?></span>
		    <input type="hidden" class='synth-salaire-form' name="instr[<?php echo $employe->matricule ?>][salaire_brute]" value="<?php echo $synthese->salaire_brute; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->cp_du_n1); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][cp_du_n1]" value="<?php echo $synthese->cp_du_n1; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->cp_acquis_n1); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][cp_acquis_n1]" value="<?php echo $synthese->cp_acquis_n1; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->prov_cp); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][prov_cp]" value="<?php echo $synthese->prov_cp; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->salaire_net); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][salaire_net]" value="<?php echo $synthese->salaire_net; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->vir1); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][vir1]" value="<?php echo $synthese->vir1; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->vir2); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][vir2]" value="<?php echo $synthese->vir2; ?>">
		</td>
    		<td>
		    <?php echo Number::frenchFormat($synthese->chq); ?>
		    <input type="hidden" name="instr[<?php echo $employe->matricule ?>][chq]" value="<?php echo $synthese->chq; ?>">
		</td>
    	    </tr>
	    <?php endforeach; ?>
	</tbody>
    </table>
</form>

<script type="text/javascript">
    $(function(){
        
        var maj_data_pk = function($oDom, value){ 
                                var $trParent = $oDom.parent().parent();
                                $trParent.find('td a').each(function(){
                                    $a = $(this);
                                    if($a.is('[data-pk]')){ // has data-pk attribute
                                        $a.attr('data-pk', value );
                                    }
                                });
                            },
            update_row = function(objCall){ 
                            var objDef = { "thisA" : null, "url": null, "value": null, 'success' : null};
                            obj = $.extend(objDef , objCall) ;
                            if( ! (obj.thisA && obj.url && obj.value) )
                                return false;
                            
                            var $thisA = obj.thisA,
                                url    = obj.url,
                                value  = obj.value;                              
                            pk      = $thisA.attr('data-pk');
                            name    = $thisA.attr('data-name');
                            data = 'pk='+pk+ '&name='+name+'&value=' + value;
                            
                            $ajax = $.ajax({
                                type: 'post', url: url, data: data, async: false
                                           });
                            $ajax.done(function(data){
                                if(obj.success)
                                    obj.success(data);
                            });               
                          },
             calculSalaire = function($linkCell, pkInst){
                var $trParent = $linkCell.parent().parent();
        
                $ajax = $.ajax({
                    type: 'post',
                    url: '<?php echo $this->url('RHX/default', array('controller' => 'instructionpaie', 'action' => 'salaire')) ?>',
                    data: '&id=' + pkInst
                });
                $ajax.done(function(result) {	   
                    var salaire = result.salaire_synth;

                    $trParent.find('td .synth-salaire-text').text( salaire );
                    $trParent.find('td .synth-salaire-form').val(salaire );
                });
             };
                          
	$('.synthese').click(function(e){
	    e.preventDefault();
	    $('.icon-arrow-down').show('slow');
	    $ajax = $.ajax({
		type: 'post',
		url : '<?php echo $this->url('RHX/default', array('controller' => 'instructionpaie', 'action' => 'synthese')) ?>',
		data : $('#form-synthese').serialize()
	    });
	    $ajax.done(function(result){
		if(result.done){
		    window.location.href = "<?php echo $this->basePath() ?>/upload/excel/" + result.filename;
		}else{
		    showError(result.error);
		}
		$('.icon-arrow-down').hide('slow');
	    });
	});
        
        $('.synth-edit-int').editable({
            title : 'Edition',
            url : function (params) {   
                var $thisA = $(this);
                update_row({
                               'thisA'  : $(this),  
                               'url'   : '<?php echo $this->url('RHX/default', array('controller' => 'instructionpaie', 'action' => 'updateNumber')) ?>',                                
                               'value' : params.value,
                               'success' : function(value){                                   
                                   calculSalaire($thisA, $thisA.attr('data-pk') );
                               }
                              }) ;
            },
            validate : function(v){
                var $thisA = $(this),
                    $itsInput = $thisA.parent().find("input[type='hidden']")    ;
                
		id      = $thisA.attr('data-id');
		name    = $thisA.attr('data-name');
                grp    = $thisA.attr('data-group');
                pk      = $thisA.attr('data-pk');
		selector = '#form-' + id + '-' + name;
                
                $itsInput.val(replaceVirgule(v));
                
                if(pk < 0){                    
                    $ajax = $.ajax({
			type: 'post',
			url: '<?php echo $this->url('RHX/default', array('controller' => 'instructionpaie', 'action' => 'insert')) ?>',
			data: '&value=' + v + '&name=' + name + '&matricule=' + id + '&group=' + grp + '&periode=<?php echo $this->periode; ?>',
			async: false
		    });
		    $ajax.done(function(result) {
                        // Maj des pk
                        var paye_id = result.paie.id;                                                                               
                        maj_data_pk($thisA, paye_id);                        
		    });                    
                }
            }
        });
    });
</script>